package com.ffapp.demo.common;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONArray;
import com.ffapp.demo.R;
import com.ffapp.demo.basis.BasisApp;
import com.ffapp.demo.bean.UploadImgData;
import com.ffapp.demo.bean.UserUploadFileBean;
import com.leon.lfilepickerlibrary.LFilePicker;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.FileUtils;
import my.LogUtil;

public class AdapterFilesEdit extends RecyclerView.Adapter<AdapterFilesEdit.ViewHolder> {

        public ArrayList<UploadImgData> mListBean = new ArrayList<>();
        Activity mContext;
        boolean isEdit = false;
        int MAX_SIZE = 6;
        private int REQUEST_CODE = 99;

        public AdapterFilesEdit(Activity mContext, ArrayList<UploadImgData> list) {
            mListBean = list;
            this.isEdit = false;
            this.mContext = mContext;
        }
        public AdapterFilesEdit(Activity mContext, ArrayList<UploadImgData> list, int maxSize, int request_code) {
            mListBean = list;
            if(mListBean == null){
                mListBean = new ArrayList<>();
            }
            this.isEdit = true;
            this.mContext = mContext;
            this.MAX_SIZE = maxSize;
            this.REQUEST_CODE = request_code;
        }
//
//        public String getParamsImgids(){
//            if(mListBean == null || mListBean.size() == 0)return "";
////            String[] ids = new String[mListBean.size()];
//            StringBuilder builder = new StringBuilder();
////            JSONArray jsonArray = new JSONArray();
//            for(int i = 0 , l = mListBean.size() ; i < l ; i++){
////                ids[i] = mListBean.get(i).getId();
////                jsonArray.add(mListBean.get(i).getId());
//                if(i == 0){
//                    builder.append("[");
//                }
//                builder.append("\"").append(mListBean.get(i).getId()).append("\"").append(",");
//            }
//            if(builder.length() > 0){
//                builder.deleteCharAt(builder.length()-1);
//                builder.append("]");
//            }
////            return jsonArray.toJSONString();
//            return  builder.toString();
//        }

    public JSONArray getParamsImgidArray(){
        if(mListBean == null || mListBean.size() == 0)return  null;
//            String[] ids = new String[mListBean.size()];
            JSONArray jsonArray = new JSONArray();
        for(int i = 0 , l = mListBean.size() ; i < l ; i++){
                jsonArray.add(mListBean.get(i).getId());
        }
        return  jsonArray;
    }

    public static JSONArray getParamsImgidArray(ArrayList<UserUploadFileBean> list){
        if(list == null || list.size() == 0)return  null;
//            String[] ids = new String[mListBean.size()];
        JSONArray jsonArray = new JSONArray();
        for(int i = 0 , l = list.size() ; i < l ; i++){
            jsonArray.add(list.get(i).getId());
        }
        return  jsonArray;
    }

        public void setData(ArrayList<UploadImgData> list) {
            mListBean = list;
            if(mListBean == null){
                mListBean = new ArrayList<>();
            }
            notifyDataSetChanged();
        }

        public void setImgId(int position,String imgId){
            UploadImgData imgData = mListBean.get(position);
            imgData.setId(imgId);
            mListBean.set(position,imgData);
        }

    public void addFile(  List<String> filePaths){
        if(filePaths == null || filePaths.size() == 0)return;
            for (int i = 0, l = filePaths.size(); i < l; i++) {
                mListBean.add(new UploadImgData("",filePaths.get(i)));
            }
        notifyDataSetChanged();
    }

    public boolean isNeedUpload(){
            if(mListBean == null || mListBean.size() == 0)return false;
        UploadImgData data;
        for (int i = 0, l = mListBean.size(); i < l; i++) {
            data = mListBean.get(i);
            if (data.isNeedpost()) {
                return true;
            }
        }
        return false;
    }

        //创建新View，被LayoutManager所调用
        @Override
        public AdapterFilesEdit.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.notice_detail_file_item, viewGroup, false);
            return new AdapterFilesEdit.ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(AdapterFilesEdit.ViewHolder viewHolder, int position) {
          viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            if(mListBean == null)return 0;
            if (isEdit) {
                if (mListBean.size() >= MAX_SIZE)
                    return MAX_SIZE;
                return mListBean.size() + 1;
            } else {
                return mListBean.size();
            }

        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.img_icon)
            public ImageView img_icon;
            @BindView(R.id.img_delete)
            public ImageView img_delete;
            @BindView(R.id.text_name)
            public TextView text_name;


            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }
            public void bind(int position){
                if (isEdit && mListBean.size() < MAX_SIZE && position == mListBean.size()) {
//                Glide.with(mContext).load(R.drawable.img_add).into(viewHolder.img_icon);
                    img_icon.setImageResource(R.drawable.publish_img_btn_upload);
                    img_delete.setVisibility(View.INVISIBLE);
                    text_name.setText("");
                    text_name.setVisibility(View.GONE);
                } else {
                    UploadImgData data = mListBean.get(position);
                   list_item.setTag(data);
//                    BasisApp.loadImg(mContext,data.getPath(),img_icon,R.drawable.base_img_default_square);
//                    img_icon.setImageResource(R.drawable.file_icon_other);
                    if(data.getImgresByname() == R.drawable.file_icon_pic){
                        BasisApp.loadImg(mContext,data.getPath(),img_icon,R.drawable.file_icon_pic);
                    }else {
                        img_icon.setImageResource(data.getImgresByname());
                    }
                   img_delete.setVisibility(isEdit ? View.VISIBLE : View.INVISIBLE);
                    img_delete.setTag(position);
//            viewHolder.list_item.setBackgroundResource(R.drawable.item_shadow_bluelight_selector);
                    text_name.setVisibility(View.VISIBLE);
                    text_name.setText(data.getFileName());
                    LogUtil.i("position--->"+position+"--->"+data.getFileName());
                }


               img_icon.setTag(R.id.img_icon, position);
            }

            @OnClick(R.id.img_icon)
            public void onItemClick(View v) {
                int position = (int) v.getTag(R.id.img_icon);
                if (isEdit && position == mListBean.size()) {

                    new LFilePicker()
                            .withActivity(mContext)
                            .withRequestCode(REQUEST_CODE)
                            .withMaxNum(MAX_SIZE - mListBean.size())
//                            .withStartPath("/storage/emulated/0/Download")
                            .withIsGreater(false)
                            .withFileSize(1024 * 1024*256)
                            .withFileFilter(".png,.gif,.jpg,.jpeg,.pdf,.doc,.docx,.xls,.xlsx,.zip".split(","))
                            .start();

                }else {
                    FileUtils.openFile(mListBean.get(position).getPath(),mContext);
                }
            }

            @OnClick(R.id.img_delete)
            public void onItemDelete(View v) {
                int position = (int) v.getTag();
//                UploadImgData data = mListBean.get(position);
                mListBean.remove(position);
               notifyDataSetChanged();
            }
        }
    }